var express = require("express");
var router = express.Router();
const db = require("../db/index.js");
const { v4: uuidv4 } = require('uuid');
// 查询课程列表 分页
router.get("/list", function (req, res, next) {
  console.log(req.query);
  // 获取参数   当前页码   当前每页数据量
  // 获取参数   当前页码   当前每页数据量
  let pageNumber = req.query.pageNumber;
  // let pageSize = parseInt(req.body.pageSize)
  let pagesize = req.query.pagesize - 0;
  // 求得sql语句中的参数
  console.log(pageNumber, pagesize);
  //  if(!pageNumber) pageNumber=1
  //  if(!pagesize) pagesize=6
  console.log(pageNumber, pagesize);
  let start = (pageNumber - 1) * pagesize;
  // sql语句
  let sql = "select * from class limit ?, ?";
  let sqlParam = [start, pagesize];
  let sqlCount = "select count(*) from class";

  var count = 0;
  // 执行查询
  db.query(sqlCount, null, (err, result) => {
    // 得到总记录数
    count = result[0]["count(*)"];
  });

  db.query(sql, sqlParam, (err, result) => {
    if (err) {
      res.send({
        code: 0,
        message: err.message,
        ok: true,
      });
    } else {
      res.send({
        code: 200,
        message: "请求成功",
        ok: true,
        totalCount: count,
        data: result,
      });
    }
  });
});
router.get("/getInfo", function (req, res, next) {
  console.log(req.query);
  let id = req.query.id;
  const sql = `select * from class where id=?`;
  db.query(sql, id, function (err, result) {
    if (err) {
      return res.send({ code: 0, message: err.message });
    } else {
      return res.send({ code: 200, message: "查询成功！", data: result[0] });
    }
  });
});
// 添加课程
router.post("/addClass", function (req, res, next) {
  let info = req.body;
  let sql = `insert into class set ?`;
  let ID = uuidv4()
    ID = ID.replace(/-/g, '')
  db.query(
   
    sql,
    {
      id:ID,
      name: info.name,
      coach: info.coach,
      img: "https://tse3-mm.cn.bing.net/th/id/OIP-C.nNaB3--HuPZLjyd7dP83TAHaHa?pid=ImgDet&rs=1",
      content: info.content,
    },
    function (err, result) {
      if (err) res.send({ message: err.message });
      res.send({ code: 200, message: "添加成功！" });
    }
  );
});
// 删除课程
router.post("/deleteClass", function (req, res, next) {
  let id = req.query.id;
  console.log(id);
  const sql = "delete from class where id=?";
  db.query(sql, id, function (err, result) {
    if (err) res.send({ code: 0, message: err.message });
    res.send({ code: 200, message: "删除成功！" });
  });
});
// 修改教练
router.post("/updateClass", function (req, res, next) {
  let info = req.body;
  const sql = `update class set coach=? where id=? `;
  db.query(sql, [info.coach, info.id], function (err, result) {
    if (err) res.send({ code: 0, message: err.message });
    res.send({ code: 200, message: "修改成功!" });
  });
});
// 练车预约
router.post("/practice", function (req, res, next) {
  console.log(req.body);
  let info = req.body;
  const sql = `select * from practice where nickname=?`;
  db.query(sql, info.nickname, function (err, results) {
    if (err) res.send({ code: 0, message: err.message });
    if (results.length > 0) {
      res.send({ code: 0, message: "你已经有预约科目了~" });
    } else {
      const sql1 = `insert into practice set ?`;
      let ID = uuidv4()
      ID = ID.replace(/-/g, '')
      db.query(
        sql1,
        {
          id:ID,
          name: info.name,
          nickname: info.nickname,
          time: info.time,
          adress: info.adress,
          img: info.img,
          coach: info.coach,
        },
        function (err, data) {
          if (err) res.send({ code: 0, message: err.message });
          res.send({ code: 200, message: "预约成功！" });
        }
      );
    }
  });
});
// 模糊查询
router.post('/search',function(req,res,next){
  let name = req.body.name
  let coach  = req.body.coach
  console.log(req.body);
  const sql =  `SELECT * FROM class where name like '%${name}%' or coach like '%${coach}%'`
  db.query(sql,function(err,results){
    if(err) {return res.send({code:0,message:err.message})} 
    res.send({code:200,message:'查询成功!',data:results})
  })
})

module.exports = router;
